<?php
  require_once("../includes/config.php");
  
    $length = 20;
    
    if( isset($_POST['page'])  && !empty($_POST['page'] ))
    {
      $page =  filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
      $start = $page*20 - 20;
      unset($_POST['page']);
    }
    else
    {
      $start = 0;
      $page = 1;
    }
   $sql = "SELECT * FROM requests WHERE owner_id = ? ";
   $sqlCount="SELECT COUNT(*) FROM requests WHERE owner_id=? ";
   $binds= array();
   $binds['user_id']= $_SESSION['user_id'];
    if(!empty($_POST['from'])&&!empty($_POST['to']))
    {
      $binds['from'] = $_POST['from'];
      $binds['to'] = $_POST['to'];
      $sql .=" and date_beginning between ? AND ? ";
	  $sqlCount .= " and date_beginning between ? AND ? "; 
    }
    if(!empty($_POST['payment_name']))
    {
      $binds['payment_name'] = $_POST['payment_name'];
      $sql .=" and apart_name=? ";
	  $sqlCount .= " and apart_name=? "; 
    }
       $sql .="ORDER BY date_beginning DESC";   
    $sql .= " LIMIT ?,?";
    $binds['start'] = $start;
    $binds['end'] = $length;
    
    $requsts = query($sql,$binds);
    $sqlCount = query($sqlCount,$binds);
    
    $totalPages = $sqlCount[0]['COUNT(*)'];
   
    render("requests_reports.php", array("title"=>"تقرير الطلبات","page"=>$page,"totalPages"=>$totalPages,"requsts"=>$requsts,"start"=>$start,"length"=>$length,"binds"=>$binds));

?>